#!/bin/sh
################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2016 Stephan Raue (stephan@openelec.tv)
#
#  OpenELEC is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 2 of the License, or
#  (at your option) any later version.
#
#  OpenELEC is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

if [ -z "$1" ]; then
  echo "Usage: $0 <branch-name>"
  exit 0
fi

KODI_DIR="kodi-$1.git"

git_clone() {
  # git_clone https://repo.url branch ./target_dir [githash]
  echo "[mkpkg] Checking out $1 ..."
  if [ ! -d "$3" ]; then
    git clone "$1" -b $2 "$3"
  else
    if [ -d "$3" ] ; then
      cd "$3"
      git checkout $2 >/dev/null 2>/dev/null
      git pull
      cd ..
    fi
  fi
  if [ ! -z "$4" ] ; then
    cd "$3"
    git fetch >/dev/null 2>/dev/null
    git branch -D $4 >/dev/null 2>/dev/null
    git checkout $4 >/dev/null 2>/dev/null
    git checkout -b ref-$4 >/dev/null 2>/dev/null
    cd ..
  fi
}

resolve_hash() {
  if [ -d "$1" ] ; then
    cd "$1"
    git rev-parse --short $2 2>/dev/null
  fi
}

if [ ! -d $KODI_DIR ] ; then
  echo "meh.. $KODI_DIR does not exist"
  exit 127
fi

# platform
REPO=$(cat $KODI_DIR/project/cmake/addons/depends/common/platform/platform.txt | awk '{print $2}')
GIT_HASH=$(cat $KODI_DIR/project/cmake/addons/depends/common/platform/platform.txt | awk '{print $3}')
PKG_NAME="platform"

git_clone $REPO master $PKG_NAME.git $GIT_HASH
if [ -f ../../packages/mediacenter/platform/package.mk ] ; then
  # update package.mk
  RESOLVED_HASH=$(resolve_hash $PKG_NAME.git $GIT_HASH)
  sed -e "s|PKG_VERSION=.*|PKG_VERSION=\"$RESOLVED_HASH\"|g" \
      -e "s|PKG_GIT_URL=.*|PKG_GIT_URL=\"$REPO\"|g" \
      -i ../../packages/mediacenter/platform/package.mk
fi
rm -rf $PKG_NAME.git

# kodi-platform
REPO=$(cat $KODI_DIR/project/cmake/addons/depends/common/kodi-platform/kodi-platform.txt | awk '{print $2}')
GIT_HASH=$(cat $KODI_DIR/project/cmake/addons/depends/common/kodi-platform/kodi-platform.txt | awk '{print $3}')
PKG_NAME="kodi-platform"

git_clone $REPO master $PKG_NAME.git $GIT_HASH
if [ -f ../../packages/mediacenter/kodi-platform/package.mk ] ; then
  # update package.mk
  RESOLVED_HASH=$(resolve_hash $PKG_NAME.git $GIT_HASH)
  sed -e "s|PKG_VERSION=.*|PKG_VERSION=\"$RESOLVED_HASH\"|g" \
      -e "s|PKG_GIT_URL=.*|PKG_GIT_URL=\"$REPO\"|g" \
      -i ../../packages/mediacenter/kodi-platform/package.mk
fi
rm -rf $PKG_NAME.git

# addons
for addontxt in $KODI_DIR/project/cmake/addons/bootstrap/repositories/*-addons.txt ; do
  ADDONS=$(cat $addontxt | awk '{print $1}')
  ADDONREPO=$(cat $addontxt | awk '{print $2}')
  GIT_HASH=$(cat $addontxt | awk '{print $3}')
  git_clone $ADDONREPO master $ADDONS.git $GIT_HASH
  for addon in $ADDONS.git/*.*/ ; do
    ADDON=$(basename $addon)
    REPO=$(cat $addon/$ADDON.txt | awk '{print $2}')
    GIT_HASH=$(cat $addon/$ADDON.txt | awk '{print $3}')
    PKG_NAME="$ADDON"

    if ! grep -q all $addon/platforms.txt && ! grep -q linux $addon/platforms.txt && ! grep -q ! $addon/platforms.txt; then
      continue
    fi

    if [ -f ../../packages/mediacenter/kodi-binary-addons/$ADDON/package.mk ] ; then
      git_clone $REPO master $PKG_NAME.git $GIT_HASH

      # update package.mk
      RESOLVED_HASH=$(resolve_hash $PKG_NAME.git $GIT_HASH)
      sed -e "s|PKG_VERSION=.*|PKG_VERSION=\"$RESOLVED_HASH\"|g" \
          -e "s|PKG_GIT_URL=.*|PKG_GIT_URL=\"$REPO\"|g" \
          -i ../../packages/mediacenter/kodi-binary-addons/$ADDON/package.mk

      rm -rf $PKG_NAME.git

    else
      echo "[mkpkg] Skipped $ADDON"
      SKIPPED_ADDONS="$SKIPPED_ADDONS $ADDON"
    fi
  done
  echo "followed addons was skipped, please add packages for this:"
  echo "$SKIPPED_ADDONS"
done
